export function dateFormat(fmt, date) {
  let ret;
  const opt = {
    "y+": date.getFullYear().toString(),        // 年
    "m+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString(),            // 日
    "H+": date.getHours().toString(),           // 时
    "M+": date.getMinutes().toString(),         // 分
    "S+": date.getSeconds().toString()          // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    }
    ;
  }
  ;
  return fmt;
}

// 时间戳转年月日
export function secondToDate(format, time) {

  var t = new Date(time);
  var tf = function (i) {
    return (i < 10 ? '0' : '') + i
  };
  return format.replace(/yyyy|mm|dd|HH|MM|SS/g, function (a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear());
        break;
      case 'mm':
        return tf(t.getMonth() + 1);
        break;
      case 'dd':
        return tf(t.getDate());
        break;
      case 'HH':
        return tf(t.getHours());
        break;
      case 'MM':
        return tf(t.getMinutes());
        break;
      case 'SS':
        return tf(t.getSeconds());
        break;
    }
  })
}

//计算年龄
export function getAge(year, month, day) {
  var nowTime = new Date();
  var age = nowTime.getFullYear() - year

  function leap(year) {
    return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)
  }

  if (month === 2 && day === 29 && !leap(nowTime.getFullYear())) {
    day = 28;
  }
  var nowBirthday = new Date(nowTime.getFullYear(), month - 1, day);
  if (nowBirthday > nowTime) {
    age--;
  }
  return age
}

//el-paicker时间转化
export function conversionTime(date) {
  let start, end, timeDate;

  //闰年
  function leap(year) {
    return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)
  }

  //开始时间
  let now = new Date(date[0]);
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let day = now.getDate();
  month = (month < 10 ? '0' + month : month)
  day = (day < 10 ? '0' + day : day)
  start = (year.toString() + '-' + month.toString() + '-' + day.toString())
  //结束时间
  let oldNow = new Date(date[1]);
  let oldYear = oldNow.getFullYear();
  let oldMonth = oldNow.getMonth() + 1;
  let oldDay = oldNow.getDate() + 1;
  oldDay = (oldDay < 10 ? '0' + oldDay : oldDay)
  //debugger
  if (oldMonth === 2 && !leap(oldYear)) {
    if (oldDay < 29) {
      end = (oldYear.toString() + '-' + oldMonth.toString() + '-' + oldDay.toString())
    } else {
      let ste = oldMonth + 1
      ste = (ste < 10 ? '0' + ste : ste)
      end = (oldYear.toString() + '-' + ste.toString() + '-' + '01')
    }
  } else if ([1, 3, 5, 7, 8, 10].findIndex(a => a === oldMonth) !== -1 && !leap(oldYear)) {
    if (oldDay < 32) {
      end = (oldYear.toString() + '-' + oldMonth.toString() + '-' + oldDay.toString())
    } else {
      let ste = oldMonth + 1
      ste = (ste < 10 ? '0' + ste : ste)
      end = (oldYear.toString() + '-' + ste.toString() + '-' + '01')
    }
  } else if ([12].findIndex(a => a === oldMonth) !== -1 ) {
    if (oldDay < 32) {
      end = (oldYear.toString() + '-' + oldMonth.toString() + '-' + oldDay.toString())
    } else {
      oldYear = oldYear + 1
      end = (oldYear.toString() + '-' + '01' + '-' + '01')
    }
  } else if ([4, 6, 9, 11].findIndex(a => a === oldMonth) !== -1 && !leap(oldYear)) {
    if (oldDay < 31) {
      end = (oldYear.toString() + '-' + oldMonth.toString() + '-' + oldDay.toString())
    } else {
      let ste = oldMonth + 1
      ste = (ste < 10 ? '0' + ste : ste)
      end = (oldYear.toString() + '-' + ste.toString() + '-' + '01')
    }
  } else if (oldMonth === 2 && leap(oldYear)) {
    if (oldDay < 30) {
      end = (oldYear.toString() + '-' + oldMonth.toString() + '-' + oldDay.toString())
    } else {
      let ste = oldMonth + 1
      ste = (ste < 10 ? '0' + ste : ste)
      end = (oldYear.toString() + '-' + ste.toString() + '-' + '01')
    }
  }
  timeDate = [start, end]
  return timeDate
}
